<?php

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/parameters_exception.php");
require_once("lib/exception/duplicate_instance_exception.php");

require_once("lib/model/action.php");

require_once("model/autor_message/dao/autor_message_dao_factory.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Send_message_action implements Action {

	private $message;
	
	public function __construct($message) {
		
		$this->message = $message;
	}
	
	public function do_execute() {
		try {
			$dao = Autor_dao_factory::singleton()->get_dao();
			if (!$dao->exist($this->message->get_to_autor_login()))
				throw new Parameters_exception('Not destinatary existent');
			$dao = Autor_message_dao_factory::singleton()->get_dao();
			$this->message->set_state('new');
			return $dao->create($this->message);
					
		}
		catch (Parameters_exception $e) {
			throw $e;
		}
		catch (Duplicate_instance_exception $e) {
			throw new Model_exception($e);
		}
		catch (Instance_not_found_exception $e) {
			throw new Model_exception($e);
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
}
?>